package org.systemsbiology.rface.hadoop.math.em;

import org.systemsbiology.util.KahanSummation;
import org.systemsbiology.util.Summator;

import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: anorberg
 * Date: 10/6/11
 * Time: 3:37 PM
 * To change this template use File | Settings | File Templates.
 *
 * Class to represent a double, and its normalized weights from each estimated mean we're working with,
 * and a mode toggle so each mean doesn't have to "know" its own ID as long as it doesn't try to screw with the indexer.
 */
public class PointWeightViewer1D {
    private double position;
    private double[] weights;
    private int currentIndex;

    public PointWeightViewer1D(double pos){
        position = pos;
        weights = null;
        currentIndex = -1;
    }

    /*package*/ void calculateWeights(Iterable<? extends EstimatedMean1D> means){
        ArrayList<Double> rawWeights = new ArrayList<Double>();
        Summator tally = new KahanSummation();
        for(EstimatedMean1D mean : means){
            double w = mean.rawWeight(position);
            rawWeights.add(w);
            tally.add(w);
        }
        if(weights == null || weights.length != rawWeights.size()){
            weights = new double[rawWeights.size()];
        }
        double t = tally.total();
        for(int k = 0; k < weights.length; ++k){
            weights[k] = rawWeights.get(k) / t;
        }
    }

    public double getPosition(){return position;}

    public double getWeight(){
        return weights[currentIndex];
    }

    /*package*/ void setIndex(int index){
        currentIndex = index;
    }
}
